package com.ff.neuropp.controller;


import com.ff.neuropp.domain.Course;
import com.ff.neuropp.domain.Order;
import com.ff.neuropp.repository.OrderRepository;
import com.ff.neuropp.service.AppCourseService;
import com.ff.neuropp.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * pc端课程信息
 */
@RestController
@RequestMapping("/api/course")
@Api(value = "pc课程controller", tags = {"pc课程接口"})
public class PcCourseController {

    @Autowired
    private AppCourseService courseService;

    @Resource
    private OrderRepository orderRepository;


    /**
     *  pc 课程列表
     * @param courseName
     * @param pageNo
     * @param pageSize
     * @param request
     * @return
     */
    @GetMapping("/listpage")
    @ApiOperation(value = "查询课程分页列表(PC)", notes = "查询课程分页列表(pc)")
    public Map<String, Object> findListPage(@ApiParam(required = false, value = "课程名称 模糊搜索") @RequestParam(required = false) String courseName,
                                            @ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                            @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize,
                                            HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        if (null == pageNo) {
            pageNo = 1;
        } else if (pageNo < 1) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        param.put("GT_id", 0);
        if (null != courseName && courseName.length() > 0) {
            param.put("LIKE_courseName", courseName);
        }
        Page<Course> list = courseService.findPage(pageNo, pageSize, param);
        if (null != list && list.getTotalElements() > 0) {
            return ResultTool.genSuccessResult4Page("分页查询成功",list);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"没有查询到数据");
        }
    }
    
    
    @PostMapping("/create")
//    @RequiredPermission("/api/course/create")
    @ApiOperation(value = "保存课程信息(PC)", notes = "保存课程信息")
    public Map<String, Object> saveCourse(@RequestBody Course course) {
        course.setCategoryName(course.getCategory().getName());
    	courseService.saveCourse(course);
        return ResultTool.genSuccessResult("添加成功");
    }
    
    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询课程信息(PC)", notes = "根据id查询课程信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Course course = courseService.findById(id);
        if (null == course) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "课程信息不存在");
        }
        return ResultTool.genSuccessResult("查询成功",course);
    }

    
    
    @PostMapping("/update")
//    @RequiredPermission("/api/course/update")
    @ApiOperation(value = "更新课程信息(PC)", notes = "更新课程信息")
    public Map<String, Object> updateRole(@RequestBody Course course) {
    	Course bak = courseService.findById(course.getId());
        if (null == bak) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "编辑错误");
        }
        courseService.saveCourse(course);
        return ResultTool.genSuccessResult("修改成功");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除课程信息", notes = "删除课程信息")
    public  Map<String,Object> deleteCourseCategory(@ApiParam(required = true,value = "课程id") @RequestParam Integer id){
        Course course = courseService.findById(id);

        //判断一下订单里面是否有这个课程 ，如果有 就提示不能删除
        if (null == course) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "无该条数据");
        }
        Order o = orderRepository.findByOrderStatusAndCourse_Id(1,id);
        if(o !=null){
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "该课程暂时无法删除");
        }



        courseService.deleteById(id);
        return ResultTool.genSuccessResult("删除成功");

    }

}
